package com.loong.user.controller.forehead;


import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.rest.Result;
import com.loong.user.common.forehead.param.WeChatAppletLoginRequestParam;
import com.loong.user.handler.WeChatAppletLoginHandler;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 微信小程序授权登录控制
 */
@RestController
@RequestMapping("user/weChat/applet")
public class WeChatAppletLoginController extends BaseForeheadController {
//    @Resource
//    AuthUtils authUtils;
//    @Resource
//    private DynamicKey dynamicKey;
    @Resource
    private WeChatAppletLoginHandler weChatAppletLoginHandler;

    /**
     * 微信小程序授权登录
     * @param requestParam
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody WeChatAppletLoginRequestParam requestParam){


       // WeChatAppletConfig dynamicValue = dynamicKey.getDynamicValue(DynamicConfigKey.WECHAT.WECHAT_APPLET_KEY, WeChatAppletConfig.class);
        //小程序相关信息
//        String appId=dynamicValue.getAppId();
//        String secret = dynamicValue.getSecret();
//        String code = requestParam.getCode();
//        String rawData = requestParam.getRawData();
//        JSONObject rawDataJson = JSONObject.parseObject(rawData);
//
//        SessionKeyOrOpenIdResult sessionKeyOrOpenId = authUtils.getSessionKeyOrOpenId(appId, secret, code);
//        String sessionKey = sessionKeyOrOpenId.getSessionKey();
//        String openid = (String)sessionKeyOrOpenId.getOpenId();
//        String encryptedData = requestParam.getEncryptedData();
//        String iv = requestParam.getIv();
//        UserEncryptedDataResult userInfo = authUtils.getUserInfo(encryptedData, sessionKey, iv);
//        System.out.println(userInfo);

        return handlerExecutor.execute(()-> weChatAppletLoginHandler.weChatAppletLogin(requestParam));

    }


}
